using DYH.CAD.Extensions.Service.Interface;
using Mapster;
using MapsterMapper;

namespace DYH.CAD.Extensions.Service.Implement;

[SingletonDI(typeof(IMapperService))]
internal class MapperService : IMapperService
{
    private static List<Assembly> _assemblies = [];

    public static void SetAssembly(IEnumerable<Assembly> assemblies)
    {
        _assemblies = assemblies.ToList();
    }

    public MapperService()
    {
        var typeAdapterConfig = new TypeAdapterConfig();
        typeAdapterConfig.Scan(_assemblies.ToArray());
        _mapper = new Mapper(typeAdapterConfig);
    }

    private readonly IMapper _mapper;

    public object MapTo(object source, object destiny)
    {
        return _mapper.Map(source, destiny, source.GetType(), destiny.GetType());
    }

    public TDestiny MapTo<TDestiny>(object obj)
    {
        return _mapper.Map<TDestiny>(obj);
    }
}